package com.zjw.zy.coreui

import android.os.Bundle
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.activityViewModels
import com.zjw.zy.R
import com.zjw.zy.base.BaseFragment
import com.zjw.zy.constant.WeatherConstant
import com.zjw.zy.coreui.dialog.OneListDialog
import com.zjw.zy.coreui.view.MainWeatherView
import com.zjw.zy.ext.go
import com.zjw.zy.ext.longToast
import com.zjw.zy.ext.mainActivity
import com.zjw.zy.utils.*
import com.zjw.zy.viewmodel.AppViewModel
import com.zjw.zy.viewmodel.WeatherViewModel
import com.zjw.zy.viewmodel.WsMessageDispatchViewModel

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/27 14:09
 */
class MainFragment : BaseFragment() {

    companion object {
        private const val TAG = "MainFragment"
    }

    private val mWeatherViewModel: WeatherViewModel by activityViewModels()
    private val wsMessageDispatchViewModel: WsMessageDispatchViewModel by activityViewModels()

    private val mAppViewModel: AppViewModel by activityViewModels()

    private val mMainContainer: ConstraintLayout by lazy { findViewById(R.id.main_fragment_content) }

    private val mMainWeatherView: MainWeatherView by lazy { findViewById(R.id.main_weather_view) }

    private var hour = TimeUtils.currentTime().hour

    private val mTimeChangeObserver = object : TimeUtils.TimeListener {
        override fun onTimeChange(date: TimeUtils.TimeBean) {
            mMainWeatherView.updateTime(date.stringOfTime)
        }

        override fun onArriveDay() {
        }

        override fun onArriveNight() {
        }

    }

    private fun updateWeatherIfNeed(date: TimeUtils.TimeBean = TimeUtils.currentTime()) {
        if (hour != date.hour) {
            hour = date.hour
            //时间更新，重新刷新
            mWeatherViewModel.request1HourWeather()
        }
    }


    override fun onPageEnter() {
        WindowUtils.TopBar.showTopBarNav()
        WindowUtils.TopBar.setTopBarLabelText(ResourcesUtils.getString(R.string.main_topbar_label))
        WindowUtils.TopBar.hideAllRightButton()
        WindowUtils.FootBar.show()
        WindowUtils.TopBar.show1RightButton(ResourcesUtils.getDrawable(R.drawable.icon_three_point)) {
            OneListDialog.get(WeatherConstant.WEATHER_CITY_MAP).bindPositiveCallback {
                SharePrefUtils.putInt(WeatherConstant.WEATHER_CURRENT_CITY_AREA_CODE_KEY, it)
                //重新请求天气
                mWeatherViewModel.request1HourWeather()
            }.title(ResourcesUtils.getString(R.string.weather_city_select_text)).selectValue(
                SharePrefUtils.getInt(
                    WeatherConstant.WEATHER_CURRENT_CITY_AREA_CODE_KEY,
                    101040100
                )
            ).show()
        }
        WeatherUtils.getCurrentWeatherBackGround()?.let {
            mainActivity().setFullBackGround(it)
        }

        updateWeatherIfNeed()
    }

    override fun onPageCreate() {
        super.onPageCreate()
        mainActivity().unImmersive()
        mWeatherViewModel.request1HourWeather()
        TimeUtils.registerTimeObserver(mTimeChangeObserver)
        wsMessageDispatchViewModel.connectService()
    }

    override fun onPageDestroy() {
        super.onPageDestroy()
        TimeUtils.unRegisterTimeObserver(mTimeChangeObserver)
    }

    override fun onViewRestore(savedInstanceState: Bundle?) {
    }

    override fun onSubscribe() {
        mWeatherViewModel.getHourWeather().observe(this) {
            it?.let {
                mMainWeatherView.setWeatherInformation(it)
                mainActivity().setFullBackGround(WeatherUtils.getWeatherBackground(it.hourlyFcsts[0]))
            }
        }
    }

    override fun onBindViewListener() {
        mMainWeatherView.bindListener(object : MainWeatherView.WeatherViewListener {
            override fun onWeather15DayClick() {
                if (mWeatherViewModel.getHourWeather().value == null) {
                    R.string.toast_error_net_work.longToast()
                    return
                }
                go(R.id.nav_main_fragment_to_nav_weather_15_fragment)
            }
        })
    }

    override fun onBindViewValue() {
    }

    override fun onFindView(rootView: View?) {
    }

    override fun onLayoutId(): Int {
        return R.layout.main_fragment
    }

    override fun onPageExit() {

    }

    override fun onBackClick() {
        super.onCallMoveToBack()
    }
}